Help with "Set-Contact" during Import
I have a couple of hundred contacts I am trying to import into Exchange 2007 SP3. I can import the contacts using shell command: "import-csv c:\import.csv | forEach { New-MailContact -Name $._displayname ....}" however, I am also trying to find a way to set the "Company" attribute with the same command, but so far I can't get it to work. I have tried this "import-csv c:\import.csv | forEach { New-MailContact -Name $._displayname ....} | set-contact -Company $_.Company however, I receive a warning for each contact during import stating "THe command completed successfully but no settings of "domain/OU/Contacts/users name" have been modified." The contacts are created but the Company attribute is not set. I have tried other combinations but cant seem to find something that works. How can I create new contacts and set attributes such as company with 1 command?
May 4th, 2012 5:43pm

How about something like this... import-csv c:\import.csv | forEach { $contact = New-MailContact -Name $._displayname -ExternalEmailAddress $_.ExternalEmailAddress Set-contact -identity $contact.name -Company $_.Company } Mike Pfeiffer | blog: mikepfeiffer.net | twitter: @mike_pfeiffer
Free Windows Admin Tool Kit Click here and download it now
May 4th, 2012 6:47pm

Hi Mac1234, Any updates? Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.Frank Wang TechNet Community Support
May 7th, 2012 3:20am

No dice with this command. I receive the following error for each user: New-MailContact : A positional parameter cannot be found that accepts argument 'Set-Contact' +CategoryInfo : InvalidArgument : (:) [New-MailContact], ParameterBindingException +FullyQualifiedErrorid: PositionalParameterNotFound
Free Windows Admin Tool Kit Click here and download it now
May 7th, 2012 2:14pm

Maybe this is not possible with one command? It would seem that contacts need to exist before the set-contact command can be run, which makes sense, but I have seen various references online that have new-mailcontact and set-contact combined. I have simply created a PS script to run the two separate commands as follows: Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin import-csv c:\import.csv | foreach { new-mailcontact -Name $_.displayName -FirstName $_.FirstName .....and so on ... } start-sleep -s 60 #this is required because without it the set-contact command runs but the system hasnt finished creating the contacts yet so it throws an 'Account doesnt exist' error for each and every contact. import-csv c:\import.csv | foreach { set-conact -identity $_.displayName -Company $_.Company } this appears to work, but for knowledge sake I am still curious if the two cmdlets can be combined. thanks
May 7th, 2012 5:13pm

On Mon, 7 May 2012 18:14:55 +0000, mac1234 wrote: > > >No dice with this command. I receive the following error for each user: > >New-MailContact : A positional parameter cannot be found that accepts argument 'Set-Contact' > >+CategoryInfo : InvalidArgument : (:) [New-MailContact], ParameterBindingException > >+FullyQualifiedErrorid: PositionalParameterNotFound Those are two separate lines of code. If you want to combine them into a single line you could separate the two with a semicolon. I.e. the "set-contact" isn't supposed to be a part of the "new-mailcontact" cmdlet. Your original example ("import-csv c:\import.csv | forEach { New-MailContact -Name $._displayname ....} | set-contact -Company $_.Company") has a misplaced "}". This might work better, but the use of "$_" may be the real problem (or at least a confusing factor): "import-csv c:\import.csv | forEach { New-MailContact -Name $._displayname .... | set-contact -Company $_.Company }" This should eliminat the "$_" problem: "import-csv c:\import.csv | forEach { $c = New-MailContact -Name $._displayname ....; set-contact $c.distinguishedname -Company $_.Company}" --- Rich Matheisen MCSE+I, Exchange MVP --- Rich Matheisen MCSE+I, Exchange MVP
Free Windows Admin Tool Kit Click here and download it now
May 7th, 2012 7:51pm

Hi Mac1234, I tested it in my lab and both MVP's cmdlets work. Did you also try to run the cmdlet in EMS directly? C:\>import-csv c:\import.csv | forEach { >>contact = New-MailContact -Name $._displayname -ExternalEmailAddress $_.ExternalEmailAddress #It works with/without the semicolon in the above cmdlet >>Set-contact -identity $contact.name -Company $_.Company >>} >> ---------- Csv file format: displayname,ExternalEmailAddress,company s1,s1@ss.com,abc s2,s2@ss.com,cdeFrank Wang TechNet Community Support
May 7th, 2012 11:26pm

This might work better, but the use of "$_" may be the real problem (or at least a confusing factor): "import-csv c:\import.csv | forEach { New-MailContact -Name $._displayname .... | set-contact -Company $_.Company }" Just tested and this code appears to work for me. Much thanks. When you say $_ may be the problem - how so?
Free Windows Admin Tool Kit Click here and download it now
May 8th, 2012 2:30pm

On Tue, 8 May 2012 18:30:02 +0000, mac1234 wrote: >This might work better, but the use of "$_" may be the real problem (or at least a confusing factor): "import-csv c:\import.csv | forEach { New-MailContact -Name > >$._displayname .... | set-contact -Company $_.Company }" > >Just tested and this code appears to work for me. Much thanks. > >When you say $_ may be the problem - how so? As I said, "or a confusing factor". You have two pipelines. The "$_" is taken to mean "self". From which pipeline is the "$_" in the "set-contact" taken? :-) --- Rich Matheisen MCSE+I, Exchange MVP --- Rich Matheisen MCSE+I, Exchange MVP
May 8th, 2012 6:01pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics